//ES EL CONSTRUCTOR
package paint;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GFrame extends Frame implements MouseListener, MouseMotionListener
{
    F n = new F ( 800, 550 );
    boolean hasnew = false;
    boolean hasline = false;
    boolean haspoly = false;
    boolean haspaint = false;
    boolean haslineA =false;
    public int rs;
    linea a    = new linea ();
    lineAlz mm = new lineAlz();
    MenuBar menuBar = new MenuBar ();
    Menu menuFile = new Menu  ( "Programa");
    Menu menuLine = new Menu  ( "Lineas");
    Menu menuColor = new Menu ( "Colores");
    MenuItem menuFileExit = new MenuItem ( "Salir",
					   new MenuShortcut ( KeyEvent.VK_X ) );
    MenuItem menuFileNew = new MenuItem ( "Nuevo",
					  new MenuShortcut ( KeyEvent.VK_N ) );
    MenuItem menuLineDraw = new MenuItem ( "Dibujar",
					   new MenuShortcut ( KeyEvent.VK_L ) );
    MenuItem menuLineAlz = new MenuItem ( "Mano Alzada" );
    MenuItem menuColorFore = new MenuItem ( "Cambiar Color" );
   // MenuItem menuColorBack = new MenuItem ( "Cambiar Color Fondo" );
    int pxm = 0, pym = 0, radio = 0;
    int an;
    float ds[] =
	{
	0};
    public GFrame ()
    {
	n.memg.setBackground ( new Color ( 150, 150, 150 ) );
	n.memg.clearRect ( 0, 0, 800, 550 );
	n.addMouseListener ( this );
	n.addMouseMotionListener ( this );
	add ( n );

	menuFileExit.addActionListener
	    (
	    new ActionListener ()
	{
	    public void actionPerformed ( ActionEvent e )
	    {
		GFrame.this.windowClosed ();

	    }
	}
	);

	menuColorFore.addActionListener
	    (
	    new ActionListener ()
	{
	    public void actionPerformed ( ActionEvent e )
	    {
		if ( GFrame.this.hasnew )
		{
		    n.memg.setColor(JColorChooser.showDialog(GFrame.this,"Selector de Color",n.memg.getColor()));
		    haspaint = false;
		}
	    }
	}
	);
/*	menuColorBack.addActionListener
	    (
	    new ActionListener ()
	{
	    public void actionPerformed ( ActionEvent e )
	    {
		if ( GFrame.this.hasnew )
		{
		    n.memg.setBackground (JColorChooser.showDialog(GFrame.this,"Selector de Color",n.memg.getBackground()));
		    n.memg.clearRect ( 0, 0, 800, 550 );
		    n.rp ();
		    menuColorBack.disable ();
		}
	    }
	}
	);*/
	menuLineDraw.addActionListener
	    (
	    new ActionListener ()
	{
	    public void actionPerformed ( ActionEvent e )
	    {
		if ( GFrame.this.hasnew )
		{
		    GFrame.this.hasline = true;
		    GFrame.this.haspoly=false;
		    GFrame.this.haslineA=false;
		//    GFrame.this.hasspray=false;
		 //   menuColorBack.disable ();
		}
	    }
	}
	);
	
	menuFileNew.addActionListener
	    (
	    new ActionListener ()
	{
	    public void actionPerformed ( ActionEvent e )
	    {
		GFrame.this.n.memg.setBackground ( Color.white );
		GFrame.this.n.memg.clearRect ( 0, 0, 800, 550 );
		GFrame.this.n.repaint ();
		GFrame.this.hasnew = true;
	//	GFrame.this.menuColorBack.enable ();
	    }
	}
	);
	
		menuLineAlz.addActionListener
	    (
	    new ActionListener ()
	{
	    public void actionPerformed ( ActionEvent e )
	    {
		if ( GFrame.this.hasnew )
		{
		    GFrame.this.haslineA=true;
		   // GFrame.this.hasspray = false;
	        GFrame.this.hasline = false;
	        GFrame.this.haspoly=false;
	   //     menuColorBack.disable ();
		    
		}
	    }
	}
	);
	
	menuFile.add  ( menuFileNew );
	menuFile.add  ( menuFileExit );
	menuLine.add  ( menuLineDraw );
	menuLine.add  ( menuLineAlz );
	menuColor.add ( menuColorFore );
//	menuColor.add ( menuColorBack );
	menuBar.add   ( menuFile );
	menuBar.add   ( menuLine );
	menuBar.add   ( menuColor );
	pack ();
	setTitle ( "Paint" );
	setMenuBar ( menuBar );
	setSize ( new Dimension ( 800, 550 ) );
	setResizable ( false );

	this.addWindowListener
	    (
	    new WindowAdapter ()
	{
	    public void windowClosing ( WindowEvent e )
	    {
		GFrame.this.windowClosed ();
	    }
	}
	);
    }

    public void mouseClicked ( MouseEvent e )
    {}

    public void mousePressed ( MouseEvent e )
    {
	if ( hasline )
	{
	    a.x = e.getX ();
	    a.y = e.getY ();
	}
	if ( haspoly )
	{
	    pxm = e.getX ();
	    pym = e.getY ();
	}
		if ( haslineA )
	{
	    mm.pxx = e.getX ();
	    mm.pyy = e.getY ();
	}
    } 

   public void mouseReleased ( MouseEvent e )
    {
	if ( hasline )
	{
 	    a.x1 = e.getX ();
	    a.y1 = e.getY ();
	    a.dibujar ( n.memg );
	    n.repaint ();
	}
	
}
    public void mouseDragged ( MouseEvent e )
    {
	if ( hasline )
	{
	    Graphics2D g = ( Graphics2D ) n.getGraphics ();
	    Color c = g.getColor ();
	    g.setStroke ( n.memg.getStroke () );
	    g.setColor ( n.memg.getColor () );
	    g.setPaint ( n.memg.getPaint () );
	    g.drawImage ( n.mem, 0, 0, n );
	    a.x1 = e.getX ();
	    a.y1 = e.getY ();
	    a.dibujar ( g );
	    g.setColor ( c );

	} 
	if ( haspoly )
	{
	    Graphics2D g = ( Graphics2D ) n.getGraphics ();
	    Color c = g.getColor ();
	    g.setStroke ( n.memg.getStroke () );
	    g.setColor ( n.memg.getColor () );
	    g.setPaint ( n.memg.getPaint () );
	    g.drawImage ( n.mem, 0, 0, n );
	    radio = ( int ) Math.sqrt ( Math.pow ( ( e.getX () - pxm ), 2 ) +
					Math.pow ( ( e.getY () - pym ), 2 ) );
	    
	}
	
   	if (haslineA)
	{
		Graphics2D g = ( Graphics2D ) n.getGraphics ();	   
	    Color c = g.getColor ();
	    g.setStroke ( n.memg.getStroke () );
	    g.setColor ( n.memg.getColor () );
	    g.setPaint ( n.memg.getPaint () );
	    g.drawImage ( n.mem, 0, 0, n );
	    mm.px=e.getX();
		mm.py=e.getY();
	    mm.dibujarAlz(n.memg);		
        }
	}
    

    public void mouseMoved ( MouseEvent e )
    {}

    public void mouseEntered ( MouseEvent e )
    {
    	setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));	
    }

    public void mouseExited ( MouseEvent e )
    {}

    
    protected void windowClosed ()
    {
	System.exit ( 0 );
    }

    public void doit ( int nl, int r, int xm, int ym, Graphics2D g )
    {
	Polygon s = new Polygon ();
	int x, y;
	float rad;
	int i;
	int step;
	step = ( int ) ( 360 / nl );
	for ( i = 0; i <= 360; i += step )
	{
	    rad = ( float ) ( i * Math.PI / 180 );
	    x = ( int ) ( xm + Math.cos ( rad ) * r );
	    y = ( int ) ( ym + Math.sin ( rad ) * r );
	    s.addPoint ( x, y );
	 }
	

    }

    

   

}

class linea
{
    public int x = 0, y = 0, x1 = 0, y1 = 0;
    public linea ()
    {}

    public void dibujar ( Graphics g )
    {
	if ( g != null )
	{
	    Graphics2D g2 = ( Graphics2D ) g;
	    g2.drawLine ( x, y, x1, y1 );
	}
    }
} 

class lineAlz
{
    public int px,py,pxx,pyy;
    public lineAlz ()
    {}

    public void dibujarAlz ( Graphics mm )
    {
	if ( mm != null )
	{
	    Graphics2D ma = (Graphics2D)mm;
	    ma.drawLine ( px, py, pxx, pyy );
	    pxx=px;
	    pyy=py;
	}
    }
}